package Mainfunction;

import javax.swing.*;
import java.sql.*;

public class InsertStudentInformation {
    private String account;
    private String password;
    private Connection connection;
    private JFrame frame;
    PreparedStatement stmt=null;
    ResultSet result=null;
    public InsertStudentInformation(JFrame frame,Connection connection,String  account,String password){
        this.account=account;
        this.password=password;
        this.connection=connection;
        this.frame=frame;

    }
    public boolean giveBool(){
        try {
            String sql = "select * from studentregister where username= ?";
            stmt = connection.prepareStatement(sql);
            stmt.setString(1, account);
            result = stmt.executeQuery();
            //查询到已有账号就是已经存在，则返回false
            if (result.next()) {
                JOptionPane.showMessageDialog(
                        frame,
                        "注册错误,已存在此账号",
                        "No",
                        JOptionPane.INFORMATION_MESSAGE);

                return false;
            }
            //插入注册的账号和密码
            String sql1 = "insert into studentregister value (?,?)";
            String sql2 = "insert into 学生基本信息表 value (?,?,?,?)";
            PreparedStatement pstmt = connection.prepareStatement(sql1);
            PreparedStatement pstmt2 = connection.prepareStatement(sql2);
            pstmt.setString(1,account);
            pstmt.setString(2,password);
            pstmt2.setString(1,account);
            pstmt2.setString(2,"null");
            pstmt2.setString(3,"null");
            pstmt2.setString(4,"null");
            int count1 = pstmt.executeUpdate();
            int count2 = pstmt2.executeUpdate();
            if(count1 ==1&&count2==1){
                return true;
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
